package com.itic.mall.mobile.everyday;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.itic.appbase.framework.common.constants.DBConst;
import com.itic.appbase.framework.utils.StringHelper;
import com.itic.appbase.framework.utils.ValueHelper;
import com.itic.mall.mobile.base.BaseMobileInterface;
import com.itic.mall.system.goods.goodsspu.persistence.model.GoodsSpu;
import com.itic.mall.system.goods.goodsspu.persistence.model.GoodsSpuExample;
import com.itic.mall.system.goods.goodsspu.persistence.model.GoodsSpuExample.Criteria;
import com.itic.mall.system.market.marketshare.persistence.model.MarketShare;
import com.itic.mall.system.market.marketshare.persistence.model.MarketShareExample;
import com.itic.mall.system.market.marketshare.service.MarketShareService;
import com.itic.mall.system.users.usersmall.persistence.model.UsersMall;

/**
 * 首页模块接口信息
 * 
 * @author 许小宝
 * @time 2017/12/01
 */
@Controller
@RequestMapping(value = "/mobile/everyday")
public class EveryDayInterface extends BaseMobileInterface {
	
	@Autowired
	private MarketShareService marketShareService;
	
	/**
	 * 轮播图管理
	 * @return
	 */
	@RequestMapping(value = "/banner")
	@ResponseBody
	public Map<String,Object> banner( HttpServletRequest request, HttpServletResponse response ){
		Map<String, Object> resultMap = new HashMap<String, Object>();
		String errorCode = PARAM_ERROR_CODE;
		String errorMsg = PARAM_ERROR_MSG;
		try {
			resultMap.put(DATA,getMarketBannersByType("mrbt"));
			errorCode = SUCCESS_ERROR_CODE;
			errorMsg = SUCCESS_ERROR_MSG;
		} catch (Exception e) {
			errorCode = SERVER_ERROR_CODE;
			errorMsg = SERVER_ERROR_MSG;
			logException("EveryDayInterface", "banner", e.toString(), "轮播图", e);
		}
		resultMap.put(CODE, errorCode);
		resultMap.put(MSG, errorMsg);
		isCrrossDomain(response);
		return resultMap;
	}
	
	/**
	 *精选
	 * @return
	 */
	@RequestMapping(value = "/jinxuan")
	@ResponseBody
	public Map<String,Object> jinxuan( HttpServletRequest request, HttpServletResponse response ){
		Map<String, Object> resultMap = new HashMap<String, Object>();
		String errorCode = PARAM_ERROR_CODE;
		String errorMsg = PARAM_ERROR_MSG;
		try {
			Map<String, Object> dataMap = new HashMap<String, Object>();

			GoodsSpuExample example = new GoodsSpuExample();
			example.setOrderByClause("UPDATE_TIME DESC");

			Criteria criteria = example.createCriteria();
			criteria.andIsEffectiveEqualTo(DBConst.TRUE);// 是否有效
			criteria.andIsShowEqualTo(DBConst.TRUE);// 是否可见
			criteria.andIsJingpingEqualTo(DBConst.TRUE);
			
			this.handlePageQueryDefault(request, example);// 分页
			int count = goodsSpuService.count(example);// 总条数
			List<GoodsSpu> list = goodsSpuService.list(example);// 数据list

			dataMap.put("total", count);
			dataMap.put("isMore", isMore(request, count));

			dataMap.put("list", ValueHelper.tranList2MapList(list,spuIgnoreProperties));

			resultMap.put(DATA, dataMap);

			errorCode = SUCCESS_ERROR_CODE;
			errorMsg = SUCCESS_ERROR_MSG;
		} catch (Exception e) {
			errorCode = SERVER_ERROR_CODE;
			errorMsg = SERVER_ERROR_MSG;
			logException("EveryDayInterface", "jinxuan", e.toString(), "京选列表", e);
		}
		resultMap.put(CODE, errorCode);
		resultMap.put(MSG, errorMsg);
		isCrrossDomain(response);
		return resultMap;
	}
	
	
	/**
	 * 微信列表
	 * @return
	 */
	@RequestMapping(value = "/list")
	@ResponseBody
	public Map<String,Object> weixin( HttpServletRequest request, HttpServletResponse response, String token, String appId, String type ){
		Map<String, Object> resultMap = new HashMap<String, Object>();
		String errorCode = PARAM_ERROR_CODE;
		String errorMsg = PARAM_ERROR_MSG;
		try {
			if( StringHelper.isNotBlank(type) ){
				Map<String, Object> dataMap = new HashMap<String, Object>();
				
				MarketShareExample example = new MarketShareExample();
				com.itic.mall.system.market.marketshare.persistence.model.MarketShareExample.Criteria criteria = example.createCriteria();
				criteria.andIsEffectiveEqualTo(DBConst.TRUE);
				criteria.andTypeEqualTo(type);
				
				this.handlePageQueryDefault(request, example);
				int count = marketShareService.count(example);
				List<MarketShare> list = marketShareService.list(example);
				
				if( StringHelper.isNotBlank(token) && StringHelper.isNotBlank(appId) ){
					UsersMall user = getUsersByTokenAndAppId(token, appId);
					if( null != user ){
						dataMap.put("headimg", user.getUserHeadImg());
					}
				}
				
				dataMap.put("list", ValueHelper.tranList2MapList(list, new String[] {"createTime","createUser", "isEffective", "updateTime", "orgCode","reorder","note","type","topImg"}));
				dataMap.put("total", count);
				dataMap.put("isMore", isMore(request,count));
				
				resultMap.put(DATA, dataMap);
				
				errorCode = SUCCESS_ERROR_CODE;
				errorMsg = SUCCESS_ERROR_MSG;
			}
			
		} catch (Exception e) {
			errorCode = SERVER_ERROR_CODE;
			errorMsg = SERVER_ERROR_MSG;
			logException("EveryDayInterface", "wxlist", e.toString(), "微信群发列表", e);
		}
		resultMap.put(CODE, errorCode);
		resultMap.put(MSG, errorMsg);
		isCrrossDomain(response);
		return resultMap;
	}
	
	
	/**
	 * 微信列表
	 * @return
	 */
	@RequestMapping(value = "/hotDetail")
	@ResponseBody
	public Map<String,Object> hotDetail( HttpServletRequest request, HttpServletResponse response, String id ){
		Map<String, Object> resultMap = new HashMap<String, Object>();
		String errorCode = PARAM_ERROR_CODE;
		String errorMsg = PARAM_ERROR_MSG;
		try {
			Map<String, Object> dataMap = new HashMap<String, Object>();
			
			//dataMap.put("banner",  getMarketBannersByType("bkfx"));
			
			MarketShare marketShare = marketShareService.get(id);
			
			dataMap.put( "banner", marketShare.getTopImg() );
			
			List<String> ids = new ArrayList<String>();
			String spuIds = marketShare.getSpuIds();
			if( StringHelper.isNotBlank(spuIds) ){
				String[] split = spuIds.split(",");
				for( int i=0; i<split.length; i++ ){
					ids.add(split[i]);
				}
			}
			
			GoodsSpuExample example = new GoodsSpuExample();
			example.setOrderByClause("UPDATE_TIME DESC");

			Criteria criteria = example.createCriteria();
			criteria.andIsEffectiveEqualTo(DBConst.TRUE);// 是否有效
			criteria.andIdIn(ids);
			
			this.handlePageQueryDefault(request, example);// 分页
			int count = goodsSpuService.count(example);// 总条数
			List<GoodsSpu> list = goodsSpuService.list(example);// 数据list

			dataMap.put("total", count);
			dataMap.put("isMore", isMore(request, count));

			dataMap.put("list", ValueHelper.tranList2MapList(list,spuIgnoreProperties));
			
			resultMap.put(DATA, dataMap);
			
			errorCode = SUCCESS_ERROR_CODE;
			errorMsg = SUCCESS_ERROR_MSG;
			
		} catch (Exception e) {
			errorCode = SERVER_ERROR_CODE;
			errorMsg = SERVER_ERROR_MSG;
			logException("EveryDayInterface", "wxlist", e.toString(), "微信群发列表", e);
		}
		resultMap.put(CODE, errorCode);
		resultMap.put(MSG, errorMsg);
		isCrrossDomain(response);
		return resultMap;
	}
	
	/**
	 * 微信分享
	 * @return
	 */
	@RequestMapping(value = "/share")
	@ResponseBody
	public Map<String,Object> share( HttpServletRequest request, HttpServletResponse response, String token, String appId, String id ){
		Map<String, Object> resultMap = new HashMap<String, Object>();
		String errorCode = PARAM_ERROR_CODE;
		String errorMsg = PARAM_ERROR_MSG;
		try {
			if( StringHelper.isNotBlank(token) && StringHelper.isNotBlank(appId) && StringHelper.isNotBlank(id) ){
				UsersMall user = getUsersByTokenAndAppId(token, appId);
				if( null != user ){
					MarketShare marketShare = marketShareService.get(id);
					if( null != marketShare ){
						Map<String, Object> dataMap = new HashMap<String, Object>();
						List<String> list = new ArrayList<String>();
						String spuIds = marketShare.getSpuIds();
						
						String[] split = spuIds.split(",");
						int length = split.length;
						if( length <= 9 ){
							List<String> posters = new ArrayList<String>();//存放海报的图片路径
							int fails = 0;
							for( int i=0; i<split.length; i++ ){
							    GoodsSpu spu = goodsSpuService.get(split[i]);
							    if( null != spu ){
							    	posters.add(getPoster(spu, user));
							    }else{
							        fails++;
							    }
							}
							//商品下架的提醒消息
							if( fails>0 ){
								dataMap.put("failure", "有"+fails + "件商品已下架了");
							}else{
								dataMap.put("failure", "");
							}
							dataMap.put("poster", posters);
							resultMap.put(DATA, dataMap );
							errorCode = SUCCESS_ERROR_CODE;
							errorMsg = SUCCESS_ERROR_MSG;
							dataMap.put("tips", "");
							dataMap.put("images", list);
							resultMap.put(DATA, dataMap);
							errorCode = SUCCESS_ERROR_CODE;
							errorMsg = SUCCESS_ERROR_MSG;
						}else{
							errorCode = MAX_NINE_ERROR_CODE;
							errorMsg = MAX_NINE_ERROR_MSG;
						}
					}else{
						marketShareService.delete(marketShare);
						errorCode = MARKET_ID_ERROR_CODE;
						errorMsg = MARKET_ID_ERROR_MSG;
					}
					
				}else{
					errorCode = LOGIN_ERROR_CODE;
					errorMsg = LOGIN_ERROR_MSG;
				}
			}
			
		} catch (Exception e) {
			errorCode = SERVER_ERROR_CODE;
			errorMsg = SERVER_ERROR_MSG;
			logException("EveryDayInterface", "wxlist", e.toString(), "微信群发列表", e);
		}
		resultMap.put(CODE, errorCode);
		resultMap.put(MSG, errorMsg);
		isCrrossDomain(response);
		return resultMap;
	}
	
	
}